package com.codeoss.framework.view.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.parser.IComponentParser;
import com.codeoss.framework.view.parser.ParseContext;
import com.codeoss.framework.view.parser.store.IComponentParserStore;
import com.codeoss.module.ir.model.IrPage;
import com.codeoss.module.ir.service.IIrPageService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Component
public class PageUtil {
	@Autowired
	private IIrPageService irPageService;
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private IComponentParserStore componentParserStore;
	public Page getPageByPageCode(YunContext yunContext,String pageCode) {
		QueryWrapper<IrPage> irPageQuery=new QueryWrapper<IrPage>();
		irPageQuery.eq("page_code", pageCode);
		IrPage irPage=irPageService.getOne(irPageQuery);
		String componentModelJson=null;
		if(irPage!=null) {
			componentModelJson=irPage.getComponentModelJson();
		}
		try {
			ParseContext parseContext=new ParseContext();
			ObjectNode modelNode=(ObjectNode) objectMapper.readTree(componentModelJson);
			IComponentParser componentParser=componentParserStore.getComponentParser(parseContext, modelNode);
			Page page=(Page)componentParser.parse(parseContext, modelNode);
			componentParser.afterPageParse(parseContext, page);
			return page;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}
